package edu.arizona.mindseye.swing;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSlider;

import edu.arizona.mindseye.Annotation;
import edu.arizona.mindseye.event.Event;
import edu.arizona.mindseye.event.EventListener;
import edu.arizona.mindseye.event.EventManager;
import edu.arizona.mindseye.event.enums.EventType;
import edu.arizona.mindseye.event.enums.Parameter;
import edu.arizona.mindseye.swing.listeners.SeekListener;

public class VideoControlPanel extends JPanel {

	private JSlider _frames;

	private JPanel _panel;
	private List<TimelinePanel> _timeLines;
	
	private JList _behaviors;
	private DefaultListModel _behaviorsModel;
	
	private String _name;
	
	public VideoControlPanel() { 
		super();
		
		addComponents();
		addListeners();
	}
	
	private void addComponents() {
		setLayout(new GridBagLayout());
		
		_frames = new JSlider(JSlider.HORIZONTAL);
//		add(_frames, GBC.makeGBC(0, 0, 2, 1, GridBagConstraints.BOTH, 1, 0));
				
		_panel = new JPanel();
		_panel.setLayout(new GridBagLayout());

		_panel.add(_frames, GBC.makeGBC(0,0, GridBagConstraints.BOTH, 1, 0));
		
		_timeLines = new ArrayList<TimelinePanel>();
		for (int i = 0; i < 3; ++i) { 
			TimelinePanel tp = new TimelinePanel(i);
			_timeLines.add(tp);
			
			tp.addTo(_panel, i+1);
		}
		
		add(_panel, GBC.makeGBC(0, 0, GridBagConstraints.BOTH, 1, 1));
		
		_behaviorsModel = new DefaultListModel();
		_behaviors = new JList(_behaviorsModel);
		add(_behaviors, GBC.makeGBC(1, 0, GridBagConstraints.BOTH, new Insets(5,5,5,5), 0.1, 0));
	}
	
	private void addListeners() { 
		_frames.addChangeListener(new SeekListener(0));

		// --- listen for events from the model...
		
		EventManager.inst().register(EventType.MOVIE_LOADED, new EventListener() {
			@Override
			public void onEvent(Event e) {
				_name = (String) e.getValue(Parameter.name);
				int frames = (Integer) e.getValue(Parameter.frames);
				
				System.out.println("Updating the frames: " + frames);
				
				_frames.setMinimum(0);
				_frames.setMaximum(frames-1);
				_frames.setValue(0);
				
				_behaviorsModel.clear();
			} 
		});
		
		EventManager.inst().register(EventType.SELECTED_ANNOTATION, new EventListener() {
			@Override
			public void onEvent(Event e) {
				int id = (Integer) e.getValue(Parameter.id);
				for (int i = 0; i < _timeLines.size(); ++i) 
					if (i != id)
						_timeLines.get(i).clearSelection();
				
				Annotation a = (Annotation) e.getValue(Parameter.annotation);
				
				_behaviorsModel.clear();
				for (String s : a.behaviors())
					_behaviorsModel.addElement(s);
			} 
		});
		
		EventManager.inst().register(EventType.DO_SAVE_ANNOTATIONS, new EventListener() {
			@Override
			public void onEvent(Event e) {
				List<Annotation> annotations = new ArrayList<Annotation>();
				for (TimelinePanel tp : _timeLines) { 
					annotations.addAll(tp.getAnnotations());
				}
				
				Event event = new Event(EventType.ANNOTATIONS);
				event.addParameter(Parameter.name, _name);
				event.addParameter(Parameter.annotation, annotations);
				EventManager.inst().dispatch(event);
			} 
			
		});
		
	}
}
